<%@ include file="../import_page.jsp"%>
<%@ page import="java.util.Calendar"%>
<%
	//login checking
	if(!loginedChecking(session, UsersType.staff)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	Units selectUnit = (Units)session.getAttribute(SESSION_SELECT_UNIT);
	String errMsg = null;
	AssignmentCollector ac = null;
	Calendar targetTime = Calendar.getInstance();
	
	//try to catch Assignment ID input
	String acIdStr = request.getParameter(PAR_ASSIGNMENT_ID);
	if(!isEmptyString(acIdStr)) {
		try {
			int acId = Integer.parseInt(acIdStr);
			AssignmentsControl aCon = new AssignmentsControl();
			ac = aCon.getAssignment(acId);
			aCon.closeDBConnection();
			//Check the unit of assignment is match the selected unit
			if(ac.getUnitID().indexOf(selectUnit.getId()) < 0)
				throw new Exception("Select Unit do not match the Assignment collector Unit");
			
			targetTime.setTime(ac.getDeadline());
		} catch (Exception err) {
			errMsg = err.toString();
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Unit Select</title>
<link rel="stylesheet" type="text/css" href="../img_style.css" />
<script type="text/javascript" src="<%= URL_JQUERY %>"></script>
<script type="text/javascript"
	src="../js/f_modify_assignment_collector.js"></script>
</head>

<body>
	<form action="<%=URL_CONFIRM_ASSIGNMENT %>" method="post"
		id="ac_modify_form">
		<input type="hidden" name="<%= PAR_ASSIGNMENT_ID %>"
			value="<%= ac == null ? "-1" : ac.getId() %>" />
		<table width="100%" border="1">
			<%if(!isEmptyString(errMsg)) { %>
			<tr>
				<td colspan="2"><%=errMsg %></td>
			</tr>
			<%} %>
			<tr>
				<td colspan="2"><p><%= ac == null ? "Create New Assignment Collector" : "Modify Assignment Collector" %></p>
				</td>
			</tr>
			<tr>
				<td bgcolor="#CCCCCC"><p>Unit Selected</p>
				</td>
				<td bgcolor="#CCCCCC"><%= selectUnit.getName() %> (<%=selectUnit.getId() %>)</td>
			</tr>
			<tr>
				<td>Assignment Name<%= ac == null ? "" : " (ID)"  %></td>
				<td><input type="text" name="<%=PAR_ASSIGNMENT_NAME %>"
					maxlength="<%=AssignmentCollector.NAME_MAX_LENGTH %>"
					value="<%= ac==null ? "" : ac.getName() %>" /><%= ac==null ? "" : "("+ ac.getId()+ ")" %>
					<span id="err_name" style="color: red;" /></td>
			</tr>
			<tr>
				<td bgcolor="#CCCCCC">DeadLine (yyyy-MM-dd HH:mm:ss)</td>
				<td bgcolor="#CCCCCC"><select name="year">
						<%=genSelectOption(2011, 2020, targetTime.get(Calendar.YEAR)) %>
				</select> - <select name="month">
						<%=genSelectOption(1, 12, targetTime.get(Calendar.MONTH) + 1) %>
				</select> - <select name="date">
						<%=genSelectOption(1, 31, targetTime.get(Calendar.DAY_OF_MONTH)) %>
				</select> <select name="hour">
						<%=genSelectOption(0, 23, targetTime.get(Calendar.HOUR_OF_DAY)) %>
				</select> : <select name="minute">
						<%=genSelectOption(0, 59, targetTime.get(Calendar.MINUTE)) %>
				</select> : <select name="second">
						<%=genSelectOption(0, 59, targetTime.get(Calendar.SECOND)) %>
				</select></td>
			</tr>
			<tr>
				<td>Assignment Status</td>
				<td><p>
						Submission <select name="isSubmission">
							<option>Close</option>
							<option
								<%= (ac != null && ac.isSubmission()) ? " selected=\"selected\"" : ""%>>Open</option>
						</select>
					</p>
					<p>
						Marking <select name="isMarking">
							<option>Close</option>
							<option
								<%= (ac != null && ac.isMarking()) ? " selected=\"selected\"" : ""%>>Open</option>
						</select>
					</p>
				</td>
			</tr>
			<tr>
				<td bgcolor="#CCCCCC">Group Size</td>
				<td bgcolor="#CCCCCC"><input type="text" name="group_size"
					maxlength="2" value="<%= ac == null ? "1" : ac.getGroupSize() %>" />
					<span id="err_groupSize" style="color: red;" /></td>
			</tr>
			<tr>
				<td bgcolor="#CCCCCC">Full Mark</td>
				<td bgcolor="#CCCCCC"><input type="text" name="full_mark"
					maxlength="7" value="<%= ac == null ? "0.00" : ac.getFullMark() %>" />
					<span id="err_mark" style="color: red;" /></td>
			</tr>
			<tr>
				<td>Description</td>
				<td><textarea name="info" cols="50" rows="5"><%= ac == null ? "" : ac.getInfo() %></textarea>
				</td>
			</tr>
			<tr>
				<td bgcolor="#CCCCCC" colspan="2"><input type="image"
					src="../images/button/submit.gif" class="img_btn" /> <a
					href="javascript:document.getElementById('ac_modify_form').reset();"><%=IMG_RESET %></a>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>
